/**
 * @Title: WechatCommettController.java
 * @Package com.leesche.zcsp.node.web.controller.wechat.commet
 * @Description:()
 * @author dennies yang
 * @date 2016年10月19日 下午12:20:51
 * @version V1.0
 */
package com.leesche.zcsp.node.web.controller.wechat.commet;

import com.leesche.zcsp.node.pay.wecha.timer.WechatTimer;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.utils.wechatutils.WechatUtil;
import com.leesche.zcsp.node.web.constants.Constants;
import com.leesche.zcsp.node.web.controller.wechat.discount.WechatDiscountController;
import com.leesche.zcsp.node.web.model.PubCommet;
import com.leesche.zcsp.node.web.service.PubCommetService;
import com.leesche.zcsp.node.web.service.PubDeviceService;
import com.leesche.zcsp.node.web.service.impl.data.read.InfoDeviceDto;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubDeviceCondition;
import com.leesche.zcsp.node.web.service.privilege.PrivilegeService;
import com.leesche.zcsp.node.web.vo.Principal;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author dennies yang
 * @version V1.0
 * @Title: WechatCommettController.java
 * @Package com.leesche.zcsp.node.web.controller.wechat.commet
 * @Description:()
 * @date 2016年10月19日 下午12:20:51
 */
@Controller
@RequestMapping("/wechat/commet")
public class WechatCommetController {
    private final static Logger LOGGER = Logger.getLogger(WechatDiscountController.class);

    @Autowired
    PubCommetService pubCommetService;

    @Autowired
    PrivilegeService privilegeService;

    @Autowired
    PubDeviceService pubDeviceService;

    private final static String MAIN_VIEW = "wechat/commet/mainView";
    private final static String FINANCE_MACHINE_VIEW = "wechat/commet/financeMachineView";

    @RequestMapping(value = "showView", method = RequestMethod.GET)
    public String showView(ModelMap model) {
        Long adminId = getAdminId();
        Integer userRoleType = privilegeService.getCurrentPrincipal().getNtype();
        QueryPubDeviceCondition condition = new QueryPubDeviceCondition();
        condition.setAdminId(adminId);
        List<InfoDeviceDto> deviceLists = pubDeviceService.findDeviceList(condition);
        model.put("deviceLists", deviceLists);

        String goView;

        switch (userRoleType) {
            //财务
            case 5:
                goView = FINANCE_MACHINE_VIEW;
                model.addAttribute("financeMachine", 1);
                model.addAttribute("machine", 0);
                break;
            //机修
            case 6:
                goView = FINANCE_MACHINE_VIEW;
                model.addAttribute("financeMachine", 0);
                model.addAttribute("machine", 1);
                break;
            //财务兼机修
            case 7:
                goView = FINANCE_MACHINE_VIEW;
                model.addAttribute("financeMachine", 1);
                model.addAttribute("machine", 1);
                break;
            default:
                goView = MAIN_VIEW;
                model.addAttribute("cur", "tucao");
                break;
        }

        return goView;
    }

    /***
     * @param @param  commet
     * @param @return
     * @return Map<String,Object>
     * @throws
     * @Title: add
     * @Description: TODO(增加吐槽)
     */
    @ResponseBody
    @RequestMapping(value = "add", method = RequestMethod.POST)
    /*public Map<String, Object> add( @RequestParam(value = "fileupload", required = false) MultipartFile file, HttpServletRequest request,String  commettitle,String  commetcontent, final PubCommet commet ){*/
    public Map<String, Object> add(HttpServletRequest request, String commettitle, String commetcontent, final PubCommet commet, String mediaid) {
        try {

            String filename = WechatUtil.downloadMedia(WechatTimer.ASSCEE_TOKEN, mediaid, Constants.SYSTEM_TMAGE_COMPLAIN_DIRECTORY);

           /* if(file==null){
                return WebResult.failure("请上传文件!").toMap();
            }
        	String path = request.getSession().getServletContext().getRealPath("upload");  
        	String fileName = file.getOriginalFilename(); 
        	System.out.println(fileName);
        	String extName = fileName.substring(fileName.lastIndexOf("."));
        	if(!WebUtils.checkIsImage(extName)){
                return WebResult.failure("必须是图片!").toMap();

        	}
        	fileName=UUIDGenerateUtil.generateUUID()+extName;
            File targetFile = new File(path, fileName);  
            if(!targetFile.exists()){  
                targetFile.mkdirs();  
            } 
            file.transferTo(targetFile);  */
            Long adminId = getAdminId();

            commet.setCommettitle(commettitle);
            commet.setCommetcontent(commetcontent);
            commet.setCommetimage(filename);
            commet.setUserid(privilegeService.getCurrentPrincipal().getUserid());
            commet.setAdminid(adminId);
            commet.setCreatetime(new Date());
            commet.setUpdatetime(new Date());
            pubCommetService.insert(commet);

            return WebResult.success().info("吐槽成功").toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }


    @ResponseBody
    @RequestMapping(value = "devices", method = RequestMethod.POST)
    public List<Map<String, Object>> getDevice() {
        try {
            Long adminId = getAdminId();

            return pubDeviceService.getDeviceListsByUserid(adminId);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return null;
        }
    }

    private Long getAdminId(){
        Principal principal = privilegeService.getCurrentPrincipal();
        Long adminId;
        if(principal.getParentid()==1){
            adminId = principal.getUserid();
        }else{
            adminId = principal.getParentid();
        }
        return adminId;
    }

}
